import { defineStore } from "pinia";

export const useAppStore = defineStore("app", () => {
    const state = reactive({
        webConfig: {
            logo: "",
            title: "",
            favicon: "",
            copyright: [] as { key: string; value: string }[],
            homeCover: "",
            selectedWorksIds: [""],
            monthlySelectionId: 0,
        }
    });

    const getWebsiteConfig = async () => {
        const { data } = await Apis.ConfigWebsite.getWebsiteConfig({});
        state.webConfig.logo = data.find(item => item.name === "pc_logo")?.value ?? "";
        state.webConfig.title = data.find(item => item.name === "pc_title")?.value ?? "";
        state.webConfig.favicon = data.find(item => item.name === "pc_favicon")?.value ?? "";
        state.webConfig.copyright = JSON.parse(data.find(item => item.name === "copyright")?.value ?? "[]");
        state.webConfig.homeCover = data.find(item => item.name === "home_cover")?.value ?? "";
        state.webConfig.selectedWorksIds = JSON.parse(data.find(item => item.name === "selected_works")?.value ?? "[]");
        state.webConfig.monthlySelectionId = JSON.parse(data.find(item => item.name === "monthly_selection")?.value ?? "0");
    };

    return {
        state,
        getWebsiteConfig
    };
});
